package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.domain.course.Course;
import edu.sdjzu.exam.domain.course.TeacherCourse;
import edu.sdjzu.exam.dto.CourseCreateDTO;
import edu.sdjzu.exam.dto.AssignTeacherDTO;
import edu.sdjzu.exam.service.course.CourseManagementService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/course-management")
@RequiredArgsConstructor
public class CourseManagementController {
    
    private final CourseManagementService courseManagementService;
    
    /**
     * 创建课程（超级管理员）
     */
    @SaCheckRole("super_admin")
    @PostMapping("/courses")
    public CommonResponse<Course> createCourse(@Valid @RequestBody CourseCreateDTO createDTO) {
        Long adminId = StpUtil.getLoginIdAsLong();
        return courseManagementService.createCourse(
            createDTO.getCourseName(),
            createDTO.getCourseCode(),
            createDTO.getDescription(),
            createDTO.getSubject(),
            adminId
        );
    }
    /**
     * 删除课程（超级管理员）
     */
    @SaCheckRole("super_admin")
    @GetMapping("deleteCourse")
    public CommonResponse<Void> deleteCourse(@RequestParam("courseId") Integer courseId){
        courseManagementService.deleteCourse(courseId);
        return CommonResponse.success("删除成功");
    }
    /**
     * 更新课程（超级管理员）
     */
    @GetMapping("updateCourse")
    public CommonResponse<Void> updateCourse(@RequestBody Course course){
        courseManagementService.updateCourse( course);
        return CommonResponse.success("更新成功");
    }
    /**
     * 查询全部课程（超级管理员）
     */
    @GetMapping("selectCourse")

    public CommonResponse<List<Course>> selectCourse(@RequestBody Course course){
        List<Course> courses= courseManagementService.selectCourse();
        return CommonResponse.success("获取成功",courses);
    }
    
    /**
     * 分配老师到课程（超级管理员）
     */
    @SaCheckRole("super_admin")
    @PostMapping("/assign")
    public CommonResponse<String> assignTeacherToCourse(@Valid @RequestBody AssignTeacherDTO assignDTO) {
        Long adminId = StpUtil.getLoginIdAsLong();
        return courseManagementService.assignTeacherToCourse(
            assignDTO.getTeacherId(),
            assignDTO.getCourseId(),
            assignDTO.getPermissionType(),
            adminId
        );
    }
    
    /**
     * 移除老师的课程权限（超级管理员）
     */
    @SaCheckRole("super_admin")
    @DeleteMapping("/assign/{teacherId}/{courseId}")
    public CommonResponse<String> removeTeacherFromCourse(
            @PathVariable Long teacherId, 
            @PathVariable Long courseId) {
        return courseManagementService.removeTeacherFromCourse(teacherId, courseId);
    }
    
    /**
     * 获取我的课程列表（老师）
     */
    @SaCheckLogin
    @GetMapping("/my-courses")
    public CommonResponse<List<Course>> getMyCourses() {
        Long teacherId = StpUtil.getLoginIdAsLong();
        List<Course> courses = courseManagementService.getTeacherCourses(teacherId);
        return CommonResponse.success("获取成功", courses);
    }
    
    /**
     * 获取所有课程列表（超级管理员）
     */
    @SaCheckRole("super_admin")
    @GetMapping("/courses")
    public CommonResponse<List<Course>> getAllCourses() {
        List<Course> courses = courseManagementService.getAllCourses();
        return CommonResponse.success("获取成功", courses);
    }
    
    /**
     * 获取课程的授权老师列表（超级管理员）
     */
    @SaCheckRole("super_admin")
    @GetMapping("/courses/{courseId}/teachers")
    public CommonResponse<List<TeacherCourse>> getCourseTeachers(@PathVariable Long courseId) {
        List<TeacherCourse> teachers = courseManagementService.getCourseTeachers(courseId);
        return CommonResponse.success("获取成功", teachers);
    }

    /**
     * 获取指定教师的课程列表（超级管理员）
     */
    @SaCheckRole("super_admin")
    @GetMapping("/teachers/{teacherId}/courses")
    public CommonResponse<List<Course>> getTeacherCourses(@PathVariable Long teacherId) {
        List<Course> courses = courseManagementService.getTeacherCourses(teacherId);
        return CommonResponse.success("获取成功", courses);
    }
}
